/**
 * Project Name:MoneyBag
 * File Name   :UserService.java
 * Package Name:com.fu.moneybag.service
 * Date        :2015年12月15日 下午10:51:18
 * Author      :Samfu
 * Blog        :http://www.fulishang.win
 * Copyright (c) 2015, flsmgf@163.com All Rights Reserved.
 *
 */
package com.fu.moneybag.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.fu.moneybag.dao.UserDao;
import com.fu.moneybag.entity.Article;
import com.fu.moneybag.entity.User;
import com.fu.moneybag.service.ArticleService;
import com.fu.moneybag.service.ArticleTypeService;
import com.fu.moneybag.service.UserService;
import com.fu.moneybag.utils.Constant;
import com.fu.moneybag.utils.FileUploadProcessor;
import com.fu.moneybag.utils.GetResultMap;
<<<<<<< HEAD
import com.fu.moneybag.utils.Page;
=======
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a

@Service
public class UserServiceImpl implements UserService {

	private Logger logger = Logger.getLogger(UserServiceImpl.class);

	@Autowired
	UserDao userDao;

	@Autowired
	ArticleService articleService;

	@Autowired
	ArticleTypeService articleTypeService;

	@Autowired
	private FileUploadProcessor fileUploadProcessor;

	private GetResultMap getResultMap;

	public GetResultMap getGetResultMap() {
		return getResultMap;
	}

	public void setGetResultMap(GetResultMap getResultMap) {
		this.getResultMap = getResultMap;
	}

	/**
	 * 添加用户（注册）
	 * 
	 * @param username
	 * @param password
	 * @param email
	 * @return User
	 */
	@Override
	public int addUser(String username, String password, String email) {
		Date createTime = new Date();
		User user = new User();
		user.setUserName(username);
		user.setPassword(password);
		user.setEmail(email);
		user.setCreateTime(createTime);
		return userDao.addUser(user);

	}

	/**
	 * 删除用户
	 * 
	 * @param userId
	 * @return Integer
	 */
	@Override
	public int deleteUserByuserId(long userId) {
		return userDao.deleteUser(userId);
	}

	/**
	 * 更新用户密码
	 * 
	 * @param userId
	 * @param password
	 */
	@Override
	public void updateUserByuserId(long userId, String password) {
		userDao.updateUserByuserId(userId, password);
	}

	// ///////////////////////////////
	// ///// 查詢 ////////
	// ///////////////////////////////
<<<<<<< HEAD
	// @Override
	// public HashMap<User, String> login(String username, String password) {
	// Map<User, String> usermap = new HashMap<User, String>();
	// User user = userDao.login(username, password);
	// // usermap.put(user, value);
	// if (user == null) {
	// usermap.put(user, Constant.LOGIN_FAILED);
	// } else {
	// usermap.put(user, Constant.LOGIN_SUCCEED);
	// }
	// return (HashMap<User, String>) usermap;
	// }
=======
//	@Override
//	public HashMap<User, String> login(String username, String password) {
//		Map<User, String> usermap = new HashMap<User, String>();
//		User user = userDao.login(username, password);
//		// usermap.put(user, value);
//		if (user == null) {
//			usermap.put(user, Constant.LOGIN_FAILED);
//		} else {
//			usermap.put(user, Constant.LOGIN_SUCCEED);
//		}
//		return (HashMap<User, String>) usermap;
//	}
	
	
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a

	// ///////////////////////////////
	// ///// 用户注册时检查用户是否已经存在 ////////
	// ///////////////////////////////
	@Override
	public User checkIfUserExist(String userName) {
		return userDao.checkUserExist(userName);
	}

	// ///////////////////////////////
	// ///// 用户注册时检查用户是否已经存在 ////////
	// ///////////////////////////////
	@Override
	public User getUserByUserName(String userName) {
		return userDao.getUserByName(userName);
	}

	@Override
	public int updateUserName(long id, String username) {
		return userDao.updateUserName(id, username);
	}

	@Override
	public User getUserById(long userId) {
		return userDao.getUserById(userId);
	}

	@Override
	public int updateUserPassword(long id, String password) {
		return userDao.updateUserPassword(id, password);
	}

	@Override
	public int updateUserEmail(long id, String email) {
		return userDao.updateUserEmail(id, email);
	}

	@Override
	public int updateUserAvartar(HttpServletRequest request, long id, MultipartFile avartarFile) {

		// 获取头像存储路径
		String avartarPath = fileUploadProcessor.fileUpload(request, avartarFile, id);

		if (avartarPath == null) {
			logger.info("头像存储地址为空。。。");
			return 0;
		} else {
			logger.info("文件名是： " + avartarPath);
			return userDao.updateUserAvartar(id, avartarPath);
		}
	}

	@Override
	public String loginService(String username, String password, HttpSession session, HttpServletRequest request) {
<<<<<<< HEAD
		// Map<User, String> usermap = this.login(username, password);
		// String loginFlag = userService.login(username, password);
		User user = this.login(username, password);
=======
		//Map<User, String> usermap = this.login(username, password);
		// String loginFlag = userService.login(username, password);
        User user = this.login(username, password);
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
		if (user == null) {
			logger.info("User: " + username + " is " + Constant.UNAUTHORITY);
			request.setAttribute("error", Constant.UNAUTHORITY);
			return "login";
		} else {

			logger.info("User " + user.getUserName() + " " + Constant.LOGIN_SUCCEED);
<<<<<<< HEAD
			// get page
			String pageNow = request.getParameter("pageNow");
			logger.info("当前的page " + pageNow);
			Page page = null;//
			// 获取文章列表
			List<Article> totalArticles = articleService.getAllArticles(user.getUserId());
			logger.info("文章总数为： " + totalArticles.size());

			// 当前也显示的文章列表
			List<Article> articles = new ArrayList<Article>();
			if (pageNow != null) {
				page = new Page(totalArticles.size(), Integer.parseInt(pageNow));
				// articles = articleService.getArticleByPage(page.getStartPos(),
				// page.getPageSize());
			} else {
				page = new Page(totalArticles.size(), 1);
				// articles = articleService.getArticleByPage(page.getStartPos(),
				// page.getPageSize());
			}

			articles = articleService.getArticleByPage(page.getStartPos(), page.getPageSize(), user.getUserId());
			session.setAttribute("articles", articles);
			session.setAttribute("page", page);
=======
			// 获取文章列表
			List<Article> articles = articleService.getAllArticles(user.getUserId());
			logger.info("文章总数为： " + articles.size());
			session.setAttribute("user", user);
			session.setAttribute("articles", articles);
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
			// 获取artilce group by article类型
			List<HashMap<String, Integer>> articleGroups = new ArrayList<HashMap<String, Integer>>();
			articleGroups = articleTypeService.getArticleGroupByType(user.getUserId());
			// 新建一个result map 给前段显示
			HashMap<String, Number> resultMap = getResultMap.getArticleMap(articleGroups);
			session.setAttribute("map", resultMap);
<<<<<<< HEAD
			session.setAttribute("user", user);
=======
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
			return "redirect:userindex";
		}
	}

	@Override
	public String indexService(HttpServletRequest request, HttpSession session) {

		User user = (User) session.getAttribute("user");
		if (user == null) {
			return "login";// session 过期，需要重新登录
		}
		logger.info("跳转到用户首页");
<<<<<<< HEAD
		//session.setAttribute("user", user);

		// get page
		String pageNow = request.getParameter("pageNow");
		logger.info("当前的page " + pageNow);

		Page page = null;//
		// 获取文章列表
		List<Article> totalArticles = articleService.getAllArticles(user.getUserId());
		logger.info("文章总数为： " + totalArticles.size());

		// 当前也显示的文章列表
		List<Article> articles = new ArrayList<Article>();
		if (pageNow != null) {
			page = new Page(totalArticles.size(), Integer.parseInt(pageNow));
			// articles = articleService.getArticleByPage(page.getStartPos(),
			// page.getPageSize());
		} else {
			page = new Page(totalArticles.size(), 1);
			// articles = articleService.getArticleByPage(page.getStartPos(),
			// page.getPageSize());
		}

		articles = articleService.getArticleByPage(page.getStartPos(), page.getPageSize(), user.getUserId());
		session.setAttribute("articles", articles);
		session.setAttribute("page", page);
=======
		session.setAttribute("user", user);
		// 获取文章列表
		List<Article> articles = articleService.getAllArticles(user.getUserId());
		logger.info("文章总数为： " + articles.size());
		session.setAttribute("articles", articles);
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
		// 获取artilce group by article类型
		List<HashMap<String, Integer>> articleGroups = new ArrayList<HashMap<String, Integer>>();
		articleGroups = articleTypeService.getArticleGroupByType(user.getUserId());
		// 新建一个result map 给前段显示
		HashMap<String, Number> resultMap = getResultMap.getArticleMap(articleGroups);
		session.setAttribute("map", resultMap);
<<<<<<< HEAD
		session.setAttribute("user", user);
		return "userindex";
	}

	// 登录时查询用户是否存在
=======
		return "userindex";
	}

	//登录时查询用户是否存在
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a
	@Override
	public User login(String username, String password) {
		return userDao.login(username, password);
	}
<<<<<<< HEAD
=======
	
	
>>>>>>> e0880f57468c829d7f55dfe5963927c05e7bee9a

}
